﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Globalization;
namespace EffiProz.Designer.Design
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [DefaultProperty("Enabled")]
    internal class Identity : IHaveConnection
    {
        private bool _isIdentity;
        private long _seed = 0;
        private long _increment = 1;
        private Column _column;

        internal Identity(Column col)
            : this(col, null)
        {
        }

        internal Identity(Column col, DataRow row)
        {
            _column = col;
            _isIdentity = false;
            if (row != null)
            {
                _isIdentity = (row.IsNull("IDENTITY") == false) ? (bool)row["IDENTITY"] : false;
                _seed = (row.IsNull("IDENTITY_START") == false) ? (long)row["IDENTITY_START"] : 0;
                _seed = (row.IsNull("IDENTITY_INCREMENT") == false) ? (long)row["IDENTITY_INCREMENT"] : 1;
            }
        }

        #region IHaveConnection Members

        [Browsable(false)]
        public ViewTableBase DesignTable
        {
            get { return _column.DesignTable; }
        }

        public DbConnection GetConnection()
        {
            return ((IHaveConnection)_column).GetConnection();
        }

        #endregion


        [DefaultValue(false)]
        [DisplayName("Enabled")]
        [RefreshProperties(RefreshProperties.All)]
        [Description("When enabled, column becomes auto-increment identity column.")]
        public bool Enabled
        {
            get { return _isIdentity; }
            set
            {
                if (value != _isIdentity)
                {
                    _isIdentity = value;
                    _column.Table._owner.MakeDirty();
                }
            }
        }

        [DefaultValue(0)]
        [DisplayName("Identity Start")]
        [RefreshProperties(RefreshProperties.All)]
        [Description("Specifies identity start value.")]
        public long IdentityStart
        {
            get { return _seed; }
            set
            {
                if (_seed != value)
                {
                    _seed = value;               
                    _column.Table._owner.MakeDirty();
                }
            }
        }

        [DefaultValue(1)]
        [DisplayName("Identity Increment")]
        [RefreshProperties(RefreshProperties.All)]
        [Description("Specifies identity increment.")]
        public long IdentityIncrement
        {
            get { return _increment; }
            set
            {
                if (_increment != value)
                {
                    _increment = value;
                    _column.Table._owner.MakeDirty();
                }
            }
        }

        public override string ToString()
        {
            if (_isIdentity == false)
                return Convert.ToString(false, CultureInfo.InvariantCulture);
            else
                return String.Format(CultureInfo.InvariantCulture, "{0} ({1},{2})", Convert.ToString(true, CultureInfo.InvariantCulture),
                    Convert.ToString(_seed, CultureInfo.InvariantCulture), Convert.ToString(_increment, CultureInfo.InvariantCulture));
            //return Convert.ToString(true);
        }
    }
   
}
